﻿using System.Collections.Generic;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Modules.Ceremony.Interfaces;
using StockholmsStad.Windows.UI.Modules.Ceremony.Models;
using StockholmsStad.Windows.UI.Modules.Ceremony.Views;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using System.Collections.ObjectModel;

namespace StockholmsStad.Windows.UI.Modules.Ceremony.Controllers
{
    public class CeremonyController : ICeremonyController
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private readonly ICeremonyService ceremonyService;

        public CeremonyController(IUnityContainer container, IRegionManager regionManager, ICeremonyService service)
        {
            this.container = container;
            this.regionManager = regionManager;
            ceremonyService = service;
        }

        #region ICeremonyController Members

        public void OnCeremonySelected(CeremonySummaryPresentationModel presentationModel)
        {
            var detailsRegion = regionManager.Regions[RegionNames.DetailsRegion];
            var contentRegion = regionManager.Regions[RegionNames.ContentRegion];
            var existingView = detailsRegion.GetView(presentationModel.Date.ToShortDateString());
            var searchView = contentRegion.GetView("CeremonyListView");

            ObservableCollection<CeremonyDetailsPresentationModel> model;
            if (presentationModel.CeremonyType == CeremonyType.Individual)
            {
                model = ceremonyService.GetIndividualCeremonyDetails(presentationModel.Date, presentationModel.Minister.MinisterId);

                //Nollställ lediga tider
                ((CeremonyView)searchView).SetFreeTimeBlocks(new List<TimeBlock>());
            }
            else
            {
                model = ceremonyService.GetOrdinaryCeremonyDetails(presentationModel.Date);

                //Hämta lediga tider för valt datum
                var freeTimeBlocks = ceremonyService.GetFreeTimeBlockDateTimes(presentationModel.Date, presentationModel.Date);
                ((CeremonyView)searchView).SetFreeTimeBlocks(freeTimeBlocks);
            }

            if (existingView == null)
                {
                    var detailsPresenter = container.Resolve<ICeremonyDetailsPresenter>();
                    detailsPresenter.SetSelectedCeremony(model);

                    detailsRegion.Add(detailsPresenter.View, presentationModel.Date.ToShortDateString());
                    detailsRegion.Activate(detailsPresenter.View);
                }
                else
                {
                    var detailsPresenter = container.Resolve<ICeremonyDetailsPresenter>();
                    detailsPresenter.SetSelectedCeremony(model);

                    var exist = existingView as CeremonyDetailsView;
                    if (exist != null) exist.Model = model;
                    detailsRegion.Activate(exist);
                }


                ///// Det verkar finnas en bugg i region-hanteringen som tvingar fram en koll om huruvida den 
                ///// existerande vyns id stämmer överens med den efterfrågade (vilket GetView borde ha grejat)
                //if (existingModel.TicketId == presentationModel.TicketId)
                //{
                    //// vyn existerar och är densamma
                    //detailsRegion.Activate(existingView);
                //}
                //else
                //{
                //    // Vyn existerar, men är inte densamma
                //    AddNewRegion(presentationModel, detailsRegion, existingView);
                //}
        }
        //    else
        //    {
        //        AddNewRegion(presentationModel, detailsRegion, existingView);
        //    }
        //}

        #endregion

        private void AddNewRegion(CeremonySummaryPresentationModel model, IRegion detailsRegion, object existingView)
        {
            if (existingView != null)
            {
                // remove previous region
                detailsRegion.Remove(existingView);
            }

            var detailsPresenter = container.Resolve<ICeremonyDetailsPresenter>();

            // TODO: Fixa rätt presentationsmodell
//            detailsPresenter.SetSelectedCeremony(model);

            detailsRegion.Add(detailsPresenter.View, model.Date.ToShortDateString(), true);
            detailsRegion.Activate(detailsPresenter.View);
        }


    }
}
